﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using University.Workshop.DataAccess;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections.ObjectModel;

namespace University.Workshop.Business
{
    public static class AnalysisReportControl
    {
        private static DataTable loadDataTable()
        {
            DataTable report1 = new DataTable() { Locale = System.Globalization.CultureInfo.CurrentCulture };

            report1.Columns.Add("Codigo Producto", Type.GetType("System.String"));
            report1.Columns.Add("Nombre Producto", Type.GetType("System.String"));
            report1.Columns.Add("Presentacion", Type.GetType("System.String"));
            report1.Columns.Add("Total", Type.GetType("System.String"));

            return report1;
            
        }
        public static DataTable GenerateQuery(DateTime dateStar, DateTime dateEnd, string typeProduct, string typeSearch)
        {
            DataTable report = new DataTable() { Locale = System.Globalization.CultureInfo.CurrentCulture };
            DataTable report1 = new DataTable() { Locale = System.Globalization.CultureInfo.CurrentCulture };

            report1 = loadDataTable();

            int result = 0;
            double result1 = 0;
            if (typeProduct == "General") {
                if (typeSearch == "Mas Vendido")
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQuery(dateStar, dateEnd);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 < Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }
                }
                else {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQuery(dateStar, dateEnd);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 > int.Parse(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }
                }
            }
            else {
                if (typeSearch == "Mas Vendido")
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQueryType(dateStar, dateEnd, typeProduct);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 < Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }
                }
                else
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQueryType(dateStar, dateEnd, typeProduct);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 > Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }
                }
            }
            
            if (report != null)
            {
                report.Dispose();
            }
            return report1;
        }
    
     

        public static Collection<string> GenerateList(DateTime dateStar, DateTime dateEnd)
        {
            DataTable report = null;
            Collection<string> list = new Collection<string>();
            report = AnalysisReportData.GeneralQuery(dateStar, dateEnd);
            for (int i = 0; i < report.Rows.Count; i++)
            {
                list.Add(report.Rows[i][3].ToString());                
            }
            return list;
            
        }
        public static Collection<string> GenerateListProduct(DateTime dateStar, DateTime dateEnd)
        {
            DataTable report = null;
            Collection<string> list = new Collection<string>();
            report = AnalysisReportData.GeneralQuery(dateStar, dateEnd);
            for (int i = 0; i < report.Rows.Count; i++)
            {
                list.Add(report.Rows[i][1].ToString());
            }
            return list;

        }

        public static bool Reports(DateTime dateStar, DateTime dateEnd, string product, string search)
        {
            DataTable report = new DataTable() { Locale = System.Globalization.CultureInfo.CurrentCulture };
            DataTable report1 = new DataTable() { Locale = System.Globalization.CultureInfo.CurrentCulture };

            report1 = loadDataTable();

            int result = 0;
            double result1 = 0;
            if (product == "General")
            {
                if (search == "Mas Vendido")
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQuery(dateStar, dateEnd);
                    foreach (DataRow ro in report.Rows)
                    {
                        result += Convert.ToInt32(ro[3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }

                    result1 = result;
                    foreach (DataRow ro in report.Rows)
                    {
                        if (0.3 < Convert.ToInt32(ro[3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = ro[0].ToString();
                            dr["Nombre Producto"] = ro[1].ToString();
                            dr["Presentacion"] = ro[2].ToString();
                            dr["Total"] = ro[3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }

                    Document document = new Document(PageSize.LETTER, 50, 40, 30, 30);

                    PdfWriter.GetInstance(document, new FileStream("ProdcutosPorVentas.pdf", FileMode.OpenOrCreate));
                    document.Open();

                    Chunk chunk1 = new Chunk("Farmacia - Productos Mas Vendidos ", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 18, iTextSharp.text.Font.UNDERLINE));
                    Chunk chunk2 = new Chunk("Cochabamba, " + DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, iTextSharp.text.Font.NORMAL));

                    Paragraph paragraph = new Paragraph();
                    Paragraph paragraph1 = new Paragraph();

                    paragraph.Add(chunk1);
                    paragraph1.Add(chunk2);
                    paragraph.Alignment = Element.ALIGN_CENTER;
                    paragraph1.Alignment = Element.ALIGN_LEFT;

                    document.Add(paragraph);
                    document.Add(new Paragraph("\n\r"));
                    document.Add(paragraph1);
                    document.Add(new Paragraph("\n\r"));

                    PdfPTable table = new PdfPTable(4);
                    PdfPCell cell = new PdfPCell();

                    cell.Colspan = 3;
                    cell.Rowspan = 1;

                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                    iTextSharp.text.Font titulos = FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.BOLD);

                    //table.AddCell(cell);
                    table.AddCell(new Phrase("CODIGO PRODUCTO", titulos));
                    table.AddCell(new Phrase("NOMBRE", titulos));
                    table.AddCell(new Phrase("PRESENTACION", titulos));
                    table.AddCell(new Phrase("CANTIDAD VENDIDA", titulos));


                    for (int a = 0; a < report1.Rows.Count; a++)
                    {

                        table.AddCell(report1.Rows[a].ItemArray[0].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[1].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[2].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[3].ToString());

                    }
                    document.Add(table);
                    document.Close();
                }
                else
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQuery(dateStar, dateEnd);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 > int.Parse(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }
                    Document document = new Document(PageSize.LETTER, 50, 40, 30, 30);

                    PdfWriter.GetInstance(document, new FileStream("ProdcutosPorVentas.pdf", FileMode.OpenOrCreate));
                    document.Open();

                    Chunk chunk1 = new Chunk("Farmacia - Productos Menos Vendidos ", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 18, iTextSharp.text.Font.UNDERLINE));
                    Chunk chunk2 = new Chunk("Cochabamba, " + DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, iTextSharp.text.Font.NORMAL));

                    Paragraph paragraph = new Paragraph();
                    Paragraph paragraph1 = new Paragraph();

                    paragraph.Add(chunk1);
                    paragraph1.Add(chunk2);
                    paragraph.Alignment = Element.ALIGN_CENTER;
                    paragraph1.Alignment = Element.ALIGN_LEFT;

                    document.Add(paragraph);
                    document.Add(new Paragraph("\n\r"));
                    document.Add(paragraph1);
                    document.Add(new Paragraph("\n\r"));

                    PdfPTable table = new PdfPTable(4);
                    PdfPCell cell = new PdfPCell();

                    cell.Colspan = 3;
                    cell.Rowspan = 1;

                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                    iTextSharp.text.Font titulos = FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.BOLD);

                    //table.AddCell(cell);
                    table.AddCell(new Phrase("CODIGO PRODUCTO", titulos));
                    table.AddCell(new Phrase("NOMBRE", titulos));
                    table.AddCell(new Phrase("PRESENTACION", titulos));
                    table.AddCell(new Phrase("CANTIDAD VENDIDA", titulos));


                    for (int a = 0; a < report1.Rows.Count; a++)
                    {

                        table.AddCell(report1.Rows[a].ItemArray[0].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[1].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[2].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[3].ToString());

                    }
                    document.Add(table);
                    document.Close();


                }
            }
            else
            {
                if (search == "Mas Vendido")
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQueryType(dateStar, dateEnd, product);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 < Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }

                    Document document = new Document(PageSize.LETTER, 50, 40, 30, 30);

                    PdfWriter.GetInstance(document, new FileStream("ProdcutosPorVentas.pdf", FileMode.OpenOrCreate));
                    document.Open();

                    Chunk chunk1 = new Chunk("Farmacia - Productos Mas Vendidos ", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 18, iTextSharp.text.Font.UNDERLINE));
                    Chunk chunk2 = new Chunk("Cochabamba, " + DateTime.Now.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, iTextSharp.text.Font.NORMAL));
                    Chunk chunk3 = new Chunk("Tipo, " + product, FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, iTextSharp.text.Font.NORMAL));

                    Paragraph paragraph = new Paragraph();
                    Paragraph paragraph1 = new Paragraph();
                    Paragraph paragraph2 = new Paragraph();

                    paragraph.Add(chunk1);
                    paragraph1.Add(chunk2);
                    paragraph2.Add(chunk3);

                    paragraph.Alignment = Element.ALIGN_CENTER;
                    paragraph1.Alignment = Element.ALIGN_LEFT;
                    paragraph2.Alignment = Element.ALIGN_LEFT;

                    document.Add(paragraph);
                    document.Add(new Paragraph("\n\r"));
                    document.Add(paragraph2);
                    document.Add(new Paragraph("\n\r"));
                    document.Add(paragraph1);
                    document.Add(new Paragraph("\n\r"));

                    PdfPTable table = new PdfPTable(4);
                    PdfPCell cell = new PdfPCell();

                    cell.Colspan = 3;
                    cell.Rowspan = 1;

                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                    iTextSharp.text.Font titulos = FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.BOLD);

                    //table.AddCell(cell);
                    table.AddCell(new Phrase("CODIGO PRODUCTO", titulos));
                    table.AddCell(new Phrase("NOMBRE", titulos));
                    table.AddCell(new Phrase("PRESENTACION", titulos));
                    table.AddCell(new Phrase("CANTIDAD VENDIDA", titulos));


                    for (int a = 0; a < report1.Rows.Count; a++)
                    {

                        table.AddCell(report1.Rows[a].ItemArray[0].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[1].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[2].ToString());
                        table.AddCell(report1.Rows[a].ItemArray[3].ToString());

                    }
                    document.Add(table);
                    document.Close();


                }
                else
                {
                    DataRow dr;
                    report = AnalysisReportData.GeneralQueryType(dateStar, dateEnd, product);
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        result = result + Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture);
                    }
                    result1 = result;
                    for (int i = 0; i < report.Rows.Count; i++)
                    {
                        if (0.3 > Convert.ToInt32(report.Rows[i][3].ToString(), System.Globalization.CultureInfo.CurrentCulture) / result1)
                        {
                            dr = report1.NewRow();
                            dr["Codigo Producto"] = report.Rows[i][0].ToString();
                            dr["Nombre Producto"] = report.Rows[i][1].ToString();
                            dr["Presentacion"] = report.Rows[i][2].ToString();
                            dr["Total"] = report.Rows[i][3].ToString();
                            report1.Rows.Add(dr);

                        }
                    }
                }
            }

            return true;
        }
    }
}
